package com.gisocn.onemap.ui.activity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.hardware.display.VirtualDisplay;
import android.media.Image;
import android.media.ImageReader;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import androidx.fragment.app.FragmentActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.bumptech.glide.Glide;
import com.gisocn.onemap.base.BaseActivity;
import com.gisocn.onemap.base.BasePresenter;
import com.gisocn.onemap.model.PhotoInfo;
import com.gisocn.onemap.sgzx.R;
import com.gisocn.onemap.utils.ImageUtils;
import com.gisocn.onemap.utils.L;
import com.gisocn.onemap.utils.SpUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

/* loaded from: classes2.dex */
public class PhotoEditActibity extends BaseActivity {
    private int currentPosition;
    private MyView doodleView;
    private int height;
    ImageReader mImageReader;
    MediaProjection mMediaProjection;
    VirtualDisplay mVirtualDisplay;
    MediaProjectionManager mediaProjectionManager;
    private View pencilColorContentView;
    private PopupWindow pencilColorPopupWindow;
    @BindView(R.id.photo_editImage)
    ImageView photoEditImage;
    @BindView(R.id.photo_seekBar)
    SeekBar seekBar;
    private int width;
    private ArrayList<PhotoInfo> photoInfoArrayList = new ArrayList<>();
    private WindowManager mWindowManager1 = null;
    private DisplayMetrics metrics = null;
    private int mScreenDensity = 0;
    private ArrayList<View> colors = new ArrayList<>();
    private boolean isCroped = false;
    private Bitmap cropBitmap = null;
    private String imageUri = "";

    @Override // com.gisocn.onemap.base.BaseActivity
    protected BasePresenter createPresenter() {
        return null;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.gisocn.onemap.base.BaseActivity, androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, androidx.activity.ComponentActivity, androidx.core.app.ComponentActivity, android.app.Activity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_photoedit);
        ButterKnife.bind(this);
        this.isCroped = false;
        WindowManager windowManager = (WindowManager) getApplication().getSystemService("window");
        this.mWindowManager1 = windowManager;
        this.width = windowManager.getDefaultDisplay().getWidth();
        this.height = this.mWindowManager1.getDefaultDisplay().getHeight();
        this.metrics = new DisplayMetrics();
        this.mWindowManager1.getDefaultDisplay().getMetrics(this.metrics);
        this.mScreenDensity = this.metrics.densityDpi;
        this.mImageReader = ImageReader.newInstance(this.width, this.height, 1, 2);
        setUpImageView();
        this.doodleView = (MyView) findViewById(R.id.doodle_view);
        SeekBar seekBar = (SeekBar) findViewById(R.id.photo_seekBar);
        this.seekBar = seekBar;
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { // from class: com.gisocn.onemap.ui.activity.PhotoEditActibity.1
            @Override // android.widget.SeekBar.OnSeekBarChangeListener
            public void onProgressChanged(SeekBar seekBar2, int i, boolean z) {
            }

            @Override // android.widget.SeekBar.OnSeekBarChangeListener
            public void onStartTrackingTouch(SeekBar seekBar2) {
            }

            @Override // android.widget.SeekBar.OnSeekBarChangeListener
            public void onStopTrackingTouch(SeekBar seekBar2) {
                PhotoEditActibity.this.doodleView.mPaint.setStrokeWidth(seekBar2.getProgress());
            }
        });
        if (Build.VERSION.SDK_INT >= 21) {
            this.mediaProjectionManager = (MediaProjectionManager) getSystemService("media_projection");
        }
    }

    public void setUpImageView() {
        this.photoInfoArrayList.clear();
        this.photoInfoArrayList.addAll((ArrayList) new Gson().fromJson(SpUtils.get(this, "photo_tag", "").toString(), new TypeToken<List<PhotoInfo>>() { // from class: com.gisocn.onemap.ui.activity.PhotoEditActibity.2
        }.getType()));
        int intExtra = getIntent().getIntExtra("Position", -1);
        this.currentPosition = intExtra;
        if (intExtra != -1) {
            this.imageUri = this.photoInfoArrayList.get(intExtra).getPhotoUrl();
            Glide.with((FragmentActivity) this).load(this.imageUri).into(this.photoEditImage);
        }
        L.e("imageUri_____________" + this.imageUri);
    }

    /* JADX WARN: Removed duplicated region for block: B:75:0x0315  */
    /* JADX WARN: Removed duplicated region for block: B:77:0x031a  */
    @butterknife.OnClick({com.gisocn.onemap.sgzx.R.id.photo_editImage, com.gisocn.onemap.sgzx.R.id.photo_doodle, com.gisocn.onemap.sgzx.R.id.photo_color, com.gisocn.onemap.sgzx.R.id.photo_trim, com.gisocn.onemap.sgzx.R.id.photo_rotate, com.gisocn.onemap.sgzx.R.id.photo_cancel, com.gisocn.onemap.sgzx.R.id.photo_recovery, com.gisocn.onemap.sgzx.R.id.photo_clean, com.gisocn.onemap.sgzx.R.id.photo_screenshots, com.gisocn.onemap.sgzx.R.id.photo_returnF, com.gisocn.onemap.sgzx.R.id.photo_returnT})
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public void onViewClicked(android.view.View r18) {
        /*
            Method dump skipped, instructions count: 1210
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: com.gisocn.onemap.ui.activity.PhotoEditActibity.onViewClicked(android.view.View):void");
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // androidx.fragment.app.FragmentActivity, android.app.Activity
    public void onActivityResult(int i, int i2, Intent intent) {
        if (i2 != -1) {
            return;
        }
        if (i == 1) {
            this.photoEditImage.setImageBitmap(BitmapFactory.decodeFile(this.photoInfoArrayList.get(this.currentPosition).getPhotoUrl()));
        } else if (i == 101 && intent != null) {
            if (Build.VERSION.SDK_INT >= 21) {
                parseData(intent, i2);
            }
        } else if (i == 5003) {
            this.isCroped = true;
            try {
                Bitmap bitmap = this.cropBitmap;
                if (bitmap != null) {
                    bitmap.recycle();
                }
                this.cropBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(ImageUtils.cropImageUri));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), this.cropBitmap);
            this.photoEditImage.setImageBitmap(null);
            this.photoEditImage.setBackground(bitmapDrawable);
        }
    }

    private void parseData(final Intent intent, final int i) {
        new Handler().postDelayed(new Runnable() { // from class: com.gisocn.onemap.ui.activity.PhotoEditActibity.3
            @Override // java.lang.Runnable
            public void run() {
                PhotoEditActibity photoEditActibity = PhotoEditActibity.this;
                photoEditActibity.mMediaProjection = photoEditActibity.mediaProjectionManager.getMediaProjection(i, intent);
                PhotoEditActibity photoEditActibity2 = PhotoEditActibity.this;
                photoEditActibity2.mVirtualDisplay = photoEditActibity2.mMediaProjection.createVirtualDisplay("screen-mirror", PhotoEditActibity.this.width, PhotoEditActibity.this.height, PhotoEditActibity.this.mScreenDensity, 16, PhotoEditActibity.this.mImageReader.getSurface(), null, null);
                new Handler().postDelayed(new Runnable() { // from class: com.gisocn.onemap.ui.activity.PhotoEditActibity.3.1
                    @Override // java.lang.Runnable
                    public void run() {
                        Image acquireLatestImage = PhotoEditActibity.this.mImageReader.acquireLatestImage();
                        if (acquireLatestImage != null) {
                            PhotoEditActibity.this.startCapture(acquireLatestImage);
                            return;
                        }
                        PhotoEditActibity.this.showToast("截屏失败");
                        if (PhotoEditActibity.this.mVirtualDisplay != null) {
                            PhotoEditActibity.this.mVirtualDisplay.release();
                            PhotoEditActibity.this.mVirtualDisplay = null;
                        }
                    }
                }, 500L);
            }
        }, 500L);
    }

    public void startCapture(Image image) {
        Log.e("imageName", "" + (System.currentTimeMillis() + ".png"));
        int width = image.getWidth();
        int height = image.getHeight();
        Log.e("getWidth", "" + width);
        Log.e("getHeight", "" + height);
        View decorView = getWindow().getDecorView();
        decorView.buildDrawingCache();
        Rect rect = new Rect();
        decorView.getWindowVisibleDisplayFrame(rect);
        int i = rect.top;
        getWindowManager().getDefaultDisplay();
        Image.Plane[] planes = image.getPlanes();
        ByteBuffer buffer = planes[0].getBuffer();
        int pixelStride = planes[0].getPixelStride();
        int rowStride = planes[0].getRowStride() - (pixelStride * width);
        StringBuilder sb = new StringBuilder();
        sb.append("");
        int i2 = (rowStride / pixelStride) + width;
        sb.append(i2);
        Log.e("createBitmap", sb.toString());
        Bitmap createBitmap = Bitmap.createBitmap(i2, height, Bitmap.Config.ARGB_8888);
        createBitmap.copyPixelsFromBuffer(buffer);
        Bitmap createBitmap2 = Bitmap.createBitmap(createBitmap, 0, 0, width, height);
        image.close();
        showToast("截屏成功");
        L.e("bitmap" + createBitmap2.getWidth());
        L.e("bitmap" + createBitmap2.getHeight());
        Glide.with((FragmentActivity) this).asBitmap().load(createBitmap2).into(this.photoEditImage);
        this.photoEditImage.setBackgroundResource(R.color.colorAccent);
        L.e("photoEditImage" + this.photoEditImage.getHeight());
        L.e("photoEditImage" + this.photoEditImage.getWidth());
        if (createBitmap2 != null) {
            VirtualDisplay virtualDisplay = this.mVirtualDisplay;
            if (virtualDisplay != null) {
                virtualDisplay.release();
                this.mVirtualDisplay = null;
            }
            saveImageToGallery(this, createBitmap2);
        }
    }

    public static void saveImageToGallery(Activity activity, Bitmap bitmap) {
        File file = new File(Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_DCIM + File.separator + "Camera" + File.separator);
        if (!file.exists()) {
            file.mkdir();
        }
        String str = System.currentTimeMillis() + ".jpg";
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(new File(file, str));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
        MediaStore.Images.Media.insertImage(activity.getContentResolver(), bitmap, str, (String) null);
        bitmap.recycle();
        activity.sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", Uri.parse("file://" + Environment.getExternalStorageDirectory().getPath())));
    }

    void colorViewClick(ArrayList<View> arrayList, final View view) {
        for (int i = 0; i < arrayList.size(); i++) {
            arrayList.get(i).setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.ui.activity.PhotoEditActibity.4
                @Override // android.view.View.OnClickListener
                public void onClick(View view2) {
                    ColorDrawable colorDrawable = (ColorDrawable) view2.getBackground();
                    PhotoEditActibity.this.doodleView.mPaint.setColor(colorDrawable.getColor());
                    PhotoEditActibity.this.pencilColorPopupWindow.dismiss();
                    view.setBackgroundColor(colorDrawable.getColor());
                }
            });
        }
    }

    private Uri stringToUri(String str) {
        Uri fromFile;
        if (Build.VERSION.SDK_INT >= 24) {
            ContentValues contentValues = new ContentValues();
            contentValues.put("_data", str);
            Log.e("contentValues", contentValues.toString());
            fromFile = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        } else {
            fromFile = Uri.fromFile(new File(str));
        }
        Log.e("stringToUri", "" + str);
        Log.e("stringToUri", "" + fromFile);
        return fromFile;
    }

    /* JADX WARN: Removed duplicated region for block: B:16:0x00a6 A[Catch: IOException -> 0x00c3, TryCatch #0 {IOException -> 0x00c3, blocks: (B:3:0x0002, B:5:0x002b, B:6:0x002e, B:9:0x0071, B:12:0x007c, B:14:0x008d, B:16:0x00a6, B:18:0x00ad, B:19:0x00b2, B:13:0x0083), top: B:24:0x0002 }] */
    /* JADX WARN: Removed duplicated region for block: B:18:0x00ad A[Catch: IOException -> 0x00c3, TryCatch #0 {IOException -> 0x00c3, blocks: (B:3:0x0002, B:5:0x002b, B:6:0x002e, B:9:0x0071, B:12:0x007c, B:14:0x008d, B:16:0x00a6, B:18:0x00ad, B:19:0x00b2, B:13:0x0083), top: B:24:0x0002 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    private android.content.Intent CutForPhoto(android.net.Uri r8) {
        /*
            r7 = this;
            java.lang.String r0 = ""
            android.content.Intent r1 = new android.content.Intent     // Catch: java.io.IOException -> Lc3
            java.lang.String r2 = "com.android.camera.action.CROP"
            r1.<init>(r2)     // Catch: java.io.IOException -> Lc3
            java.lang.StringBuilder r2 = new java.lang.StringBuilder     // Catch: java.io.IOException -> Lc3
            r2.<init>()     // Catch: java.io.IOException -> Lc3
            long r3 = java.lang.System.currentTimeMillis()     // Catch: java.io.IOException -> Lc3
            r2.append(r3)     // Catch: java.io.IOException -> Lc3
            java.lang.String r3 = ".jpg"
            r2.append(r3)     // Catch: java.io.IOException -> Lc3
            java.lang.String r2 = r2.toString()     // Catch: java.io.IOException -> Lc3
            java.io.File r3 = new java.io.File     // Catch: java.io.IOException -> Lc3
            java.lang.String r4 = com.gisocn.onemap.base.Constant.PHOTO_PATH     // Catch: java.io.IOException -> Lc3
            r3.<init>(r4, r2)     // Catch: java.io.IOException -> Lc3
            boolean r2 = r3.exists()     // Catch: java.io.IOException -> Lc3
            if (r2 == 0) goto L2e
            r3.delete()     // Catch: java.io.IOException -> Lc3
        L2e:
            r3.createNewFile()     // Catch: java.io.IOException -> Lc3
            java.lang.String r2 = "imageUri"
            java.lang.StringBuilder r4 = new java.lang.StringBuilder     // Catch: java.io.IOException -> Lc3
            r4.<init>()     // Catch: java.io.IOException -> Lc3
            r4.append(r0)     // Catch: java.io.IOException -> Lc3
            r4.append(r8)     // Catch: java.io.IOException -> Lc3
            java.lang.String r4 = r4.toString()     // Catch: java.io.IOException -> Lc3
            android.util.Log.e(r2, r4)     // Catch: java.io.IOException -> Lc3
            android.net.Uri r2 = android.net.Uri.fromFile(r3)     // Catch: java.io.IOException -> Lc3
            java.lang.String r3 = "outputUri"
            java.lang.StringBuilder r4 = new java.lang.StringBuilder     // Catch: java.io.IOException -> Lc3
            r4.<init>()     // Catch: java.io.IOException -> Lc3
            r4.append(r0)     // Catch: java.io.IOException -> Lc3
            r4.append(r2)     // Catch: java.io.IOException -> Lc3
            java.lang.String r0 = r4.toString()     // Catch: java.io.IOException -> Lc3
            android.util.Log.e(r3, r0)     // Catch: java.io.IOException -> Lc3
            java.lang.String r0 = "crop"
            r3 = 1
            r1.putExtra(r0, r3)     // Catch: java.io.IOException -> Lc3
            java.lang.String r0 = android.os.Build.MODEL     // Catch: java.io.IOException -> Lc3
            java.lang.String r4 = "EDI-AL10"
            boolean r0 = r0.contains(r4)     // Catch: java.io.IOException -> Lc3
            java.lang.String r4 = "aspectY"
            java.lang.String r5 = "aspectX"
            if (r0 != 0) goto L83
            java.lang.String r0 = android.os.Build.MODEL     // Catch: java.io.IOException -> Lc3
            java.lang.String r6 = "HUAWEI"
            boolean r0 = r0.contains(r6)     // Catch: java.io.IOException -> Lc3
            if (r0 == 0) goto L7c
            goto L83
        L7c:
            r1.putExtra(r5, r3)     // Catch: java.io.IOException -> Lc3
            r1.putExtra(r4, r3)     // Catch: java.io.IOException -> Lc3
            goto L8d
        L83:
            r0 = 9998(0x270e, float:1.401E-41)
            r1.putExtra(r5, r0)     // Catch: java.io.IOException -> Lc3
            r0 = 9999(0x270f, float:1.4012E-41)
            r1.putExtra(r4, r0)     // Catch: java.io.IOException -> Lc3
        L8d:
            java.lang.String r0 = "outputX"
            r4 = 300(0x12c, float:4.2E-43)
            r1.putExtra(r0, r4)     // Catch: java.io.IOException -> Lc3
            java.lang.String r0 = "outputY"
            r1.putExtra(r0, r4)     // Catch: java.io.IOException -> Lc3
            java.lang.String r0 = "scale"
            r1.putExtra(r0, r3)     // Catch: java.io.IOException -> Lc3
            java.lang.String r0 = "return-data"
            r4 = 0
            r1.putExtra(r0, r4)     // Catch: java.io.IOException -> Lc3
            if (r8 == 0) goto Lab
            java.lang.String r0 = "image/*"
            r1.setDataAndType(r8, r0)     // Catch: java.io.IOException -> Lc3
        Lab:
            if (r2 == 0) goto Lb2
            java.lang.String r8 = "output"
            r1.putExtra(r8, r2)     // Catch: java.io.IOException -> Lc3
        Lb2:
            java.lang.String r8 = "noFaceDetection"
            r1.putExtra(r8, r3)     // Catch: java.io.IOException -> Lc3
            java.lang.String r8 = "outputFormat"
            android.graphics.Bitmap$CompressFormat r0 = android.graphics.Bitmap.CompressFormat.JPEG     // Catch: java.io.IOException -> Lc3
            java.lang.String r0 = r0.toString()     // Catch: java.io.IOException -> Lc3
            r1.putExtra(r8, r0)     // Catch: java.io.IOException -> Lc3
            return r1
        Lc3:
            r8 = move-exception
            r8.toString()
            r8.printStackTrace()
            r8 = 0
            return r8
        */
        throw new UnsupportedOperationException("Method not decompiled: com.gisocn.onemap.ui.activity.PhotoEditActibity.CutForPhoto(android.net.Uri):android.content.Intent");
    }

    private Bitmap toConformBitmap(Bitmap bitmap, Bitmap bitmap2) {
        if (bitmap == null) {
            return null;
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int width2 = bitmap2.getWidth();
        int height2 = bitmap2.getHeight();
        L.e("bgWidth" + width);
        L.e("bgHeight" + height);
        L.e("fgWidth" + width2);
        L.e("fgHeight" + height2);
        Bitmap createBitmap = Bitmap.createBitmap(width2, width2, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        canvas.drawBitmap(bitmap, 0.0f, 0.0f, (Paint) null);
        new Paint().setStyle(Paint.Style.STROKE);
        canvas.drawBitmap(bitmap2, 0.0f, 0.0f, (Paint) null);
        canvas.save();
        canvas.restore();
        return createBitmap;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.gisocn.onemap.base.BaseActivity, androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, android.app.Activity
    public void onDestroy() {
        super.onDestroy();
        Bitmap bitmap = this.cropBitmap;
        if (bitmap != null) {
            bitmap.recycle();
        }
    }
}
